package org.dromara.book.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.book.domain.bo.SzlyBaseLanguageBo;
import org.dromara.book.domain.vo.SzlyBaseLanguageVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.book.domain.vo.SzlyBaseLocationVo;
import org.dromara.book.domain.bo.SzlyBaseLocationBo;
import org.dromara.book.service.ISzlyBaseLocationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 馆藏地
 * 前端访问路由地址为:/book/baseLocation
 *
 * @author LionLi
 * @date 2024-03-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/baseLocation")
public class SzlyBaseLocationController extends BaseController {

    private final ISzlyBaseLocationService szlyBaseLocationService;

    /**
     * 查询馆藏地列表
     */
    @SaCheckPermission("book:baseLocation:list")
    @GetMapping("/list")
    public TableDataInfo<SzlyBaseLocationVo> list(SzlyBaseLocationBo bo, PageQuery pageQuery) {
        return szlyBaseLocationService.queryPageList(bo, pageQuery);
    }

    /**
     * 馆藏地下拉选择
     */
    @GetMapping("/list2Select")
    public R<List<SzlyBaseLocationVo>> list2Select() {
        return R.ok(szlyBaseLocationService.queryList(new SzlyBaseLocationBo()));
    }

    /**
     * 导出馆藏地列表
     */
    @SaCheckPermission("book:baseLocation:export")
    @Log(title = "馆藏地", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SzlyBaseLocationBo bo, HttpServletResponse response) {
        List<SzlyBaseLocationVo> list = szlyBaseLocationService.queryList(bo);
        ExcelUtil.exportExcel(list, "馆藏地", SzlyBaseLocationVo.class, response);
    }

    /**
     * 获取馆藏地详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("book:baseLocation:query")
    @GetMapping("/{id}")
    public R<SzlyBaseLocationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(szlyBaseLocationService.queryById(id));
    }

    /**
     * 新增馆藏地
     */
    @SaCheckPermission("book:baseLocation:add")
    @Log(title = "馆藏地", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SzlyBaseLocationBo bo) {
        return toAjax(szlyBaseLocationService.insertByBo(bo));
    }

    /**
     * 修改馆藏地
     */
    @SaCheckPermission("book:baseLocation:edit")
    @Log(title = "馆藏地", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SzlyBaseLocationBo bo) {
        return toAjax(szlyBaseLocationService.updateByBo(bo));
    }

    /**
     * 删除馆藏地
     *
     * @param ids 主键串
     */
    @SaCheckPermission("book:baseLocation:remove")
    @Log(title = "馆藏地", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(szlyBaseLocationService.deleteWithValidByIds(List.of(ids), true));
    }
}
